package com.ewanghuiju.app.ui.login.activity;

import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.login.PhoneRegistbindContract;
import com.ewanghuiju.app.model.bean.request.PhoneRegistbindRequestBean;
import com.ewanghuiju.app.model.bean.response.PhoneRegistbindResponBean;
import com.ewanghuiju.app.presenter.login.PhoneRegistbindPresenter;
import com.ewanghuiju.app.ui.web.MyWebviewActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.EditTextWithDel;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;

import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 手机号注册和手机号绑定页面
 * @time 2020/2/14
 */
public class PhoneRegistbindActivity extends BaseActivity<PhoneRegistbindPresenter> implements PhoneRegistbindContract.View {
    @BindView(R.id.et_phone)
    EditTextWithDel mEtphone;
    @BindView(R.id.et_code)
    EditTextWithDel et_code;
    @BindView(R.id.login_text)
    TextView loginText;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.center_view)
    LinearLayout centerView;
    @BindView(R.id.checkbox)
    AppCompatCheckBox checkbox;
    @BindView(R.id.layout_xieyi)
    LinearLayout layoutXieyi;
    @BindView(R.id.tv_first_pwd_lab)
    TextView tvFirstPwdLab;
    @BindView(R.id.et_first_pwd)
    EditTextWithDel etFirstPwd;
    @BindView(R.id.tv_sencord_pwd_lab)
    TextView tvSencordPwdLab;
    @BindView(R.id.et_sencord_pwd)
    EditTextWithDel etSencordPwd;
    @BindView(R.id.tv_phone_lab)
    TextView tvPhoneLab;
    @BindView(R.id.tv_code_lab)
    TextView tvCodeLab;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    private int mType; //1  注册 2 忘记密码

    private Animation shake;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_phone_registbind;
    }

    @Override
    protected void initEventAndData() {
        shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        mType = getIntent().getIntExtra(Constants.LOGIN_TYPE, 1);
        if (mType == 1) {
            loginText.setText("请注册手机号码");
            tvLogin.setText("下一步");
        } else if (mType == 2) {
            loginText.setText("修改密码");
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @OnClick({R.id.tv_login, R.id.tv_code, R.id.tv_to_login, R.id.tv_yhxy, R.id.tv_yszc})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_login:
                loginByPhoneCode();
                break;
            case R.id.tv_code:
                //获取验证码
                getCode();
                break;
            case R.id.tv_to_login:
                finish();
                break;
            case R.id.tv_yhxy:
                //用户协议
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_USERXY)
                        .putExtra(Constants.WEBURL_TITLE, "用户协议").startActivity(true);
                break;
            case R.id.tv_yszc:
                //隐私政策
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_USERXY)
                        .putExtra(Constants.WEBURL_TITLE, "隐私政策").startActivity(true);
                break;
            default:
                break;
        }
    }

    /**
     * 获取验证码
     */
    private void getCode() {
        if (!checkbox.isChecked()) {
            StyleableToast.makeText(mContext, "请选择用户协议", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        String phoneNum = mEtphone.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            StyleableToast.makeText(mContext, "请输入手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        if (!StringUtil.isPhone(phoneNum)) {
            StyleableToast.makeText(mContext, "请输入正确手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        LoadingDialogUtils.show(mContext);
        if (mType == 1) {
            mPresenter.getCodeWithReg(phoneNum.trim(), "reg");
        } else {
            mPresenter.getCode(phoneNum.trim());
        }
    }

    /**
     * 验证码登录
     */
    private String phoneNum;

    private void loginByPhoneCode() {
        phoneNum = "";
        if (!checkbox.isChecked()) {
            StyleableToast.makeText(mContext, "请选择用户协议", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        phoneNum = mEtphone.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            StyleableToast.makeText(mContext, "请输入手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
//            setTipTextRest();
//            tvPhoneLab.setVisibility(View.VISIBLE);
//            tvPhoneLab.setText("请输入手机号");
//            tvPhoneLab.startAnimation(shake);
            return;
        }
        if (!StringUtil.isPhone(phoneNum)) {
            StyleableToast.makeText(mContext, "请输入正确手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        String codeStr = et_code.getText().toString();
        if (TextUtils.isEmpty(codeStr)) {
            StyleableToast.makeText(mContext, "请输入验证码", Toast.LENGTH_SHORT, R.style.mytoast).show();
//            setTipTextRest();
//            tvCodeLab.setVisibility(View.VISIBLE);
//            tvCodeLab.setText("请输入验证码");
//            tvCodeLab.startAnimation(shake);
            return;
        }

        String firstPwdStr = etFirstPwd.getText().toString();
        if (TextUtils.isEmpty(firstPwdStr)) {
            StyleableToast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT, R.style.mytoast).show();
//            setTipTextRest();
//            tvFirstPwdLab.setVisibility(View.VISIBLE);
//            tvFirstPwdLab.setText("请输入密码");
//            tvFirstPwdLab.startAnimation(shake);
            return;
        }
        String sencordPwd = etSencordPwd.getText().toString();
        if (TextUtils.isEmpty(sencordPwd)) {
            StyleableToast.makeText(mContext, "再次输入密码", Toast.LENGTH_SHORT, R.style.mytoast).show();
//            setTipTextRest();
//            tvSencordPwdLab.setVisibility(View.VISIBLE);
//            tvSencordPwdLab.setText("再次输入密码");
//            tvSencordPwdLab.startAnimation(shake);
            return;
        }
        if (!firstPwdStr.equals(sencordPwd)) {
            StyleableToast.makeText(mContext, "两次输入密码不一致", Toast.LENGTH_SHORT, R.style.mytoast).show();
//            setTipTextRest();
//            tvSencordPwdLab.setVisibility(View.VISIBLE);
//            tvSencordPwdLab.setText("两次输入密码不一致");
//            tvSencordPwdLab.startAnimation(shake);
            return;
        }
        PhoneRegistbindRequestBean phoneRegistbindRequestBean = new PhoneRegistbindRequestBean();
        phoneRegistbindRequestBean.setMobile(phoneNum.trim());
        phoneRegistbindRequestBean.setCode(codeStr.trim());
        phoneRegistbindRequestBean.setPwd(firstPwdStr.trim());
        phoneRegistbindRequestBean.setPwd1(sencordPwd.trim());
        LoadingDialogUtils.show(mContext);
        if (mType == 1) {
            mPresenter.phoneRegist(phoneRegistbindRequestBean);
        } else if (mType == 2) {
            mPresenter.Editpwd(phoneRegistbindRequestBean);
        }
    }

    /**
     * 重置提示lab
     */
    public void setTipTextRest() {
        tvPhoneLab.setVisibility(View.INVISIBLE);
        tvCodeLab.setVisibility(View.INVISIBLE);
        tvFirstPwdLab.setVisibility(View.INVISIBLE);
        tvSencordPwdLab.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (shake != null) {
            shake.cancel();
            shake = null;
        }
        if (mPresenter != null) {
            mPresenter.stopInterval();
        }
    }

    @Override
    public void showRegist(PhoneRegistbindResponBean bean) {
        //注册成功  to登录
        LoadingDialogUtils.dismissDialog_ios();
        if (mType == 1) {
//            StyleableToast.makeText(mContext, "注册成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
//            onBackPressedSupport();
            //去绑定邀请码
            new StartActivityUtil(mContext, AddInvitecodeActivity.class).putExtra(Constants.REGIST_PHONE, phoneNum).startActivity(true);
        } else if (mType == 2) {
            StyleableToast.makeText(mContext, "修改密码成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
            onBackPressedSupport();
        }
    }

    @Override
    public void showRegistResponseMsg(int code, String message) {
        StyleableToast.makeText(mContext, message, Toast.LENGTH_SHORT, R.style.mytoast).show();
    }

    @Override
    public void showBind(PhoneRegistbindResponBean bean) {

    }

    @Override
    public void showBindResponseMsg(int code, String message) {

    }

    @Override
    public void showCodeText(String text) {
        if (tvCode != null)
            tvCode.setText(text);
    }

    @Override
    public void showCodeComplete() {
        if (tvCode != null) {
            tvCode.setText("重新获取");
            tvCode.setClickable(true);
            tvCode.setTextColor(ContextCompat.getColor(mContext, R.color.text_primary));
        }
    }

    @Override
    public void showCodeOn() {
        LoadingDialogUtils.dismissDialog_ios();
        if (tvCode != null) {
            tvCode.setClickable(false);
            tvCode.setTextColor(ContextCompat.getColor(mContext, R.color.color_999));
        }
    }

    @Override
    public void showCodeResponseMsg(int code, String message) {

    }
}
